<template>
  <div class="detail">
    <product-param :title="product.name"></product-param>
    <div class="wrapper">
      <div class="container clearfix">
        <div class="swiper fl">
          <swiper :options="swiperOptions">
            <swiper-slide><img src="../../public/imgs/detail/phone-1.jpg" alt=""></swiper-slide>
            <swiper-slide><img src="../../public/imgs/detail/phone-2.jpg" alt=""></swiper-slide>
            <swiper-slide><img src="../../public/imgs/detail/phone-3.jpg" alt=""></swiper-slide>
            <swiper-slide><img src="../../public/imgs/detail/phone-4.jpg" alt=""></swiper-slide>
            <div class="swiper-pagination" slot="pagination"></div>
          </swiper>
        </div>
        <div class="content fr">
          <h2 class="item-title">{{product.name}}</h2>
          <p class="item-info">相机全新升级 / 960帧超慢动作 / 手持超级夜景 / 全球首款双频GPS / 骁龙845处理器 /<br> 红
            外人脸解锁 / AI变焦双摄 / 三星 AMOLED 屏</p>
          <div class="delivery">小米自营</div>
          <div class="item-price">{{product.price}}<span><s>1999元</s></span></div>
          <div class="line"></div>
          <div class="item-addr">
            <i class="icon-loc"></i>
            <div class="addr">北京 北京市 朝阳区 安定门街道</div>
            <div class="stock">有现货</div>
          </div>
          <div class="item-version clearfix">
            <h3>选择版本</h3>
            <div class="phone fl" :class="{'checked' : version ==1}" @click="version=1">6GB+64GB 全网通</div>
            <div class="phone fr" :class="{'checked' : version ==2}" @click="version=2">4GB+64GB 移动4G</div>
          </div>
          <div class="item-color">
            <h3>选择颜色</h3>
            <div class="phone checked"><span class="color"></span>深空灰</div>
          </div>
          <div class="item-total">
            <div class="phone-info clearfix">
              <div class="fl">{{product.name}} {{version==1 ?' 6GB+64GB 全网通':'4GB+64GB 移动4G'}} 全网通 深灰色</div>
              <div class="fr">{{product.price}}</div>
            </div>
            <div class="phone-total">总计：{{product.price}}元</div>
          </div>
          <div class="btn-group">
            <a href="javascript:;" class="btn btn-huge fl" @click="addCart">加入购物车</a>
          </div>
        </div>
      </div>
    </div>
    <div class="price-info">
      <div class="container">
        <h2>价格说明</h2>
        <div class="desc">
          <img src="../../public/imgs/detail/item-price.jpeg" alt="">
        </div>
      </div>
    </div>
    <service-bar></service-bar>
  </div>
</template>

<script>
  import ProductParam from "../components/ProductParam";
  import ServiceBar from "../components/ServiceBar";
  import {Swiper, SwiperSlide} from 'vue-awesome-swiper';
  import 'swiper/css/swiper.min.css';

  export default {
    name: "detail",
    data() {
      return {
        err: '',
        id: this.$route.params.id,// 获取商品id
        version: 1,// 商品版本切换
        product: {},// 商品信息
        swiperOptions: {
          loop: false,
          pagination: {
            el: '.swiper-pagination',
            clickable: true,
          },
        }
      }
    },
    components: {
      ProductParam,
      Swiper,
      SwiperSlide,
      ServiceBar,
    },
    mounted() {
      this.getProductInfo();
    },
    methods: {
      getProductInfo() {
        this.axios.get(`/products/${this.id}`).then((res) => {
          this.product = res;
        })
      },
      addCart() {
        this.axios.post('/carts', {
          productId: this.id,
          selected: true
        }).then((res = {cartProductVoList: 0}) => {
          this.$store.dispatch('saveCartCount', res.cartTotalQuantity);
          this.$router.push('/cart');
        }).catch((res) => {
          this.err = res;
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
  @import "../assets/scss/button";

  .wrapper {
    .swiper {
      width: 642px;
      height: 617px;

      img {
        width: 100%;
        height: 100%;
      }
    }

    .content {
      width: 584px;
      height: 870px;

      .item-title {
        font-size: 28px;
        color: #333333;
      }

      .item-info {
        font-size: 14px;
        color: #999999;
        padding-top: 14px;
      }

      .delivery {
        font-size: 16px;
        color: #FF6600;
        padding-top: 26px;
      }

      .item-price {
        font-size: 20px;
        padding-top: 14px;
        color: #FF6600;

        span {
          color: #999999;
          font-size: 14px;
          margin-left: 10px;
        }
      }

      .line {
        height: 1px;
        background-color: #B0B0B0;
        margin-top: 25px;
      }

      .item-addr {
        height: 108px;
        padding-top: 31px;
        padding-left: 64px;
        box-sizing: border-box;
        margin-top: 28px;
        border: 1px solid #B0B0B0;
        position: relative;
        font-size: 14px;
        line-height: 14px;

        .icon-loc {
          display: inline-block;
          width: 18px;
          height: 20px;
          background: url("../../public/imgs/detail/icon-loc.png") no-repeat 50%;
          background-size: cover;
          position: absolute;
          top: 27px;
          left: 34px;
        }

        .addr {
          color: #666666;
        }

        .stock {
          color: #FF6600;
          margin-top: 15px;
        }
      }

      .item-version, .item-color {
        margin-top: 30px;

        h3 {
          font-size: 18px;
          color: #333333;
        }

        .phone {
          width: 287px;
          height: 50px;
          line-height: 50px;
          text-align: center;
          font-size: 16px;
          border: 1px solid #E5E5E5;
          cursor: pointer;
          margin-top: 20px;

          .color {
            display: inline-block;
            width: 14px;
            height: 14px;
            background-color: #666666;
            margin-right: 9px;
          }
        }

        .checked {
          border-color: #FF6600;
          color: #FF6600;
        }
      }

      .item-total {
        height: 108px;
        margin-top: 50px;
        margin-bottom: 30px;
        padding: 24px 33px 29px 30px;
        box-sizing: border-box;

        .phone-info {
          font-size: 14px;
          color: #333333;
        }

        .phone-total {
          font-size: 24px;
          color: #FF6600;
          padding-top: 18px;
        }
      }
    }
  }

  .price-info {
    h2 {
      font-size: 24px;
      color: #333;
      padding-top: 38px;
      margin-bottom: 30px;
    }
  }


</style>
